package com.study.commen.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.study.commen.R;


public class NormalWebDialog extends DialogFragment implements View.OnClickListener {

    static NormalWebDialog agreementFragmentDialog;
    public static String mUrl;
    private WebView webview;

    public static NormalWebDialog newInstance(String url) {
        mUrl = url;
        if (agreementFragmentDialog == null) {
            agreementFragmentDialog = new NormalWebDialog();
        }
        return agreementFragmentDialog;
    }

    private OnDisMiss mDismiss;

    public interface OnDisMiss {
        void dismiss();
    }

    public void setOnDisMiss(OnDisMiss miss) {
        this.mDismiss = miss;
    }


    private boolean mIntercept = true;

    public void setInterceptIndex(boolean intercept) {
        mIntercept = intercept;
    }

    private OnClickListener mOnclick;

    public interface OnClickListener {
        void onHome();
    }

    public void setOnClickListener(OnClickListener miss) {
        this.mOnclick = miss;
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mDismiss != null) {
            mDismiss.dismiss();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.MyMinDialogWidth);
    }

    @JavascriptInterface
    public void closeBtnAction() {
        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dismiss();
                }
            });
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.dialog_aboutus, null);
        webview = view.findViewById(R.id.webview);
        //无网处理
        ImageView iv_back = view.findViewById(R.id.iv_back);
        final TextView tv_title = view.findViewById(R.id.tv_title);
        RelativeLayout rl_top = view.findViewById(R.id.rl_top);
        tv_title.setText("加载中..");
        webview.loadUrl(mUrl);
//        ProgressBar progressView = view.findViewById(R.id.progress);
//        webview.setprogressView(progressView);
        iv_back.setOnClickListener(this);
        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView webView, String s) {
                super.onReceivedTitle(webView, s);
                tv_title.setText(s);
            }
        });
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String url) {
                webView.loadUrl(url);
                return true;
            }
        });
        return view;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            dismiss();
        }
    }
}
